package cn.ycc1.baseset.classes;

/**
 * Providing Constructors for your Classes
 * @author ycc
 * @date 2025/3/4
 *
 * Both constructors could have been declared in Bicycle because they have different argument lists. As with methods,
 * the Java platform differentiates constructors on the basis of the number of arguments in the list and their types.
 * You cannot write two constructors that have the same number and type of arguments for the same class,
 * because the compiler would not be able to tell them apart. Doing so causes a compile-time error.
 *
 * You do not have to provide any constructors for your class, but you must be careful when doing this.
 * The compiler automatically provides a no-argument, default constructor for any class without constructors.
 * This default constructor will call the no-argument constructor of the superclass. In this situation,
 * the compiler will complain if the superclass does not have a no-argument constructor so you must verify that it does.
 * If your class has no explicit superclass, then it has an implicit superclass of Object,
 * which does have a no-argument constructor.
 *
 * You can use a superclass constructor yourself. The MountainBike class at the beginning of this lesson did just that.
 * This will be discussed later, in the lesson on interfaces and inheritance.
 *
 * You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.
 *
 * 这两个构造函数都可以在Bicycle中声明，因为它们有不同的参数列表。与方法一样，Java平台根据列表中的参数数量及其类型来区分构造函数。
 * 您不能为同一类编写两个具有相同数量和类型参数的构造函数，因为编译器无法将它们区分开来。这样做会导致编译时错误。
 * 您不必为类提供任何构造函数，但在这样做时必须小心。编译器会自动为任何没有构造函数的类提供一个无参数的默认构造函数。
 * 此默认构造函数将调用超类的无参数构造函数。在这种情况下，如果超类没有无参数构造函数，编译器将发出抱怨，因此您必须验证它是否有。
 * 如果你的类没有显式的超类，那么它有一个隐式的Object超类，它确实有一个无参数构造函数。
 * 你可以自己使用超类构造函数。本课开始时的山地自行车课就是这样做的。这将在稍后的接口和继承课程中讨论。
 * 您可以在构造函数的声明中使用访问修饰符来控制哪些其他类可以调用构造函数。
 */
public class Constructors {
    public int cadence;
    public int gear;
    public int speed;

    /**
     * Defining a Constructor
     * @param startCadence
     * @param startSpeed
     * @param startGear
     * A class contains constructors that are invoked to create objects from the class blueprint.
     * Constructor declarations look like method declarations—except that they use the name of the class
     * and have no return type. For example, Bicycle has one constructor:
     * Bicycle myBike = new Bicycle(30, 0, 8);
     */
    public Constructors(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }

    // To create a new Bicycle object called myBike, a constructor is called by the new operator:
    Constructors myBike = new Constructors(30, 0, 8);

//    The code new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.
//
//    Although Bicycle only has one constructor, it could have others, including a no-argument constructor:
    public Constructors() {
        gear = 1;
        cadence = 10;
        speed = 0;
    }
    // The code Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object
    // called yourBike.

}
